package com.wh.accesscontrol.controller;

import com.wh.accesscontrol.entity.Employee;
import com.wh.accesscontrol.entity.Stadium;
import com.wh.accesscontrol.service.lmpl.EmployeeServiceImpl;
import com.wh.accesscontrol.service.lmpl.StadiumServiceImpl;
import com.wh.accesscontrol.util.RedisUtil;
import com.wh.accesscontrol.util.RetUtil;
import com.wh.accesscontrol.util.RoleCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RoleCheck(roles = {"1"})
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StadiumServiceImpl stadiumService;
    @Autowired
    private EmployeeServiceImpl employeeServiceImpl;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired

    private HttpServletRequest request;
    @RequestMapping("/dayView")
    public Map<String, Object> dayView() {
        String token = request.getHeader("access_token");
        int id = (int) redisUtil.get(token);
        List<Stadium> stadiums = stadiumService.listByDay(id);
        List<Map<String, Object>> list1 = new ArrayList<>();
        List<Map<String, Object>> list2 = new ArrayList<>();
        List<Map<String, Object>> list3 = new ArrayList<>();
        List<Map<String, Object>> list4 = new ArrayList<>();
        List<Map<String, Object>> list5 = new ArrayList<>();
        List<Map<String, Object>> list6 = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap<>();

        for (Stadium s : stadiums) {

            Employee employee = employeeServiceImpl.findByNumber(s.getStadiumid());
            Map<String, Object> map = new HashMap<>();
            String type = String.valueOf(s.getType());

            if (type.equals("10")) {
                map.put("id", s.getStadiumid());
                map.put("inputtime", s.getEntering());
                map.put("outputtime", s.getDeparture());
                map.put("name", employee.getStadiumname());
                list4.add(map);
                hashMap.put("schoolgate", list4);
                continue;
            }
            if (type.substring(0, 1).equals("1")) {
                map.put("id", s.getStadiumid());
                map.put("inputtime", s.getEntering());
                map.put("outputtime", s.getDeparture());
                map.put("name", employee.getStadiumname());
                list1.add(map);
                hashMap.put("classroom", list1);

            }
            if (type.substring(0, 1).equals("2")) {

                map.put("id", s.getStadiumid());
                map.put("inputtime", s.getEntering());
                map.put("outputtime", s.getDeparture());
                map.put("name", employee.getStadiumname());
                list2.add(map);
                hashMap.put("library", list2);
            }
            if (type.substring(0, 1).equals("3")) {
                map.put("id", s.getStadiumid());
                map.put("inputtime", s.getEntering());
                map.put("outputtime", s.getDeparture());
                map.put("name", employee.getStadiumname());
                list3.add(map);
                hashMap.put("gymnasium", list3);
            }

            if (type.substring(0, 1).equals("4")) {
                map.put("id", s.getStadiumid());
                map.put("inputtime", s.getEntering());
                map.put("outputtime", s.getDeparture());
                map.put("name", employee.getStadiumname());
                list5.add(map);
                hashMap.put("dorm", list5);
            }


        }
        return RetUtil.retSuccess(hashMap);
    }

    @RequestMapping("/weekView")
    public Map<String, Object> weekView() {
        List<Stadium> stadiums = stadiumService.listByWeek(1602010133);
        List<Map<String, Object>> list1 = new ArrayList<>();
        List<Map<String, Object>> list2 = new ArrayList<>();
        List<Map<String, Object>> list3 = new ArrayList<>();
        List<Map<String, Object>> list4 = new ArrayList<>();
        List<Map<String, Object>> list5 = new ArrayList<>();
        List<Map<String, Object>> list6 = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        for (Stadium s : stadiums) {
            Employee employee = employeeServiceImpl.findByNumber(s.getStadiumid());
            Map<String, Object> map = new HashMap<>();
            String type = String.valueOf(s.getType());

            if (type.equals("10")) {
                map.put("id", s.getStadiumid());
                map.put("inputtime", s.getEntering());
                map.put("outputtime", s.getDeparture());
                map.put("name", employee.getStadiumname());
                list4.add(map);
                hashMap.put("schoolgate", list4);
                continue;
            }
            if (type.substring(0, 1).equals("1")) {
                map.put("id", s.getStadiumid());
                map.put("inputtime", s.getEntering());
                map.put("outputtime", s.getDeparture());
                map.put("name", employee.getStadiumname());
                list1.add(map);
                hashMap.put("classroom", list1);

            }
            if (type.substring(0, 1).equals("2")) {

                map.put("id", s.getStadiumid());
                map.put("inputtime", s.getEntering());
                map.put("outputtime", s.getDeparture());
                map.put("name", employee.getStadiumname());
                list2.add(map);
                hashMap.put("library", list2);
            }
            if (type.substring(0, 1).equals("3")) {
                map.put("id", s.getStadiumid());
                map.put("inputtime", s.getEntering());
                map.put("outputtime", s.getDeparture());
                map.put("name", employee.getStadiumname());
                list3.add(map);
                hashMap.put("gymnasium", list3);
            }

            if (type.substring(0, 1).equals("4")) {
                map.put("id", s.getStadiumid());
                map.put("inputtime", s.getEntering());
                map.put("outputtime", s.getDeparture());
                map.put("name", employee.getStadiumname());
                list5.add(map);
                hashMap.put("dorm", list5);
            }


        }
        return RetUtil.retSuccess(hashMap);
    }

}
